package com.core.website.api;

import com.core.website.common.Result;
import com.core.website.common.ResultUtil;
import com.core.website.entity.Content;
import com.core.website.entity.User;
import com.core.website.service.LoginService;
import com.core.website.util.IpAddressUtil;
import com.core.website.util.VerifyCodeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.*;

@RestController
public class LoginApi {
    public static Map<String, Date> heartbeatIpList=new HashMap<>();
    @Autowired
    private LoginService loginService;
    @PostMapping("/login")
    public Result login(@RequestBody User user, HttpSession httpSession){
//        String code = (String) httpSession.getAttribute("code");
//        if(StringUtil.isEmpty(code)|| StringUtil.isEmpty(user.getCode())){
//            return ResultUtil.error(-1,"验证码为空!");
//        }
//        if(!code.equals(StringUtils.toRootUpperCase(user.getCode()))){
//            return ResultUtil.error(-1,"验证码错误!");
//        }
        return loginService.login(user);
    }
    @GetMapping("/logout")
    public Result logout(){
        Subject subject = SecurityUtils.getSubject();
        if(null!=subject){
            subject.logout();
        }
        return ResultUtil.success();
    }
    @RequestMapping("getLoginCode")
    public void getImage(HttpSession session, HttpServletResponse response) throws IOException {
        //生成验证码
        String code = VerifyCodeUtils.generateVerifyCode(4);
        //验证码存入session
        session.setAttribute("code", code);
        //验证码存入图片
        ServletOutputStream os = response.getOutputStream();
        response.setContentType("image/png");
        VerifyCodeUtils.outputImage(220, 60, os, code);
    }
    @RequestMapping("/heartbeat")
    public Result heartbeat( HttpServletRequest request){
        String ipAddress = IpAddressUtil.getIpAddr(request);
        if(StringUtils.isNotEmpty(ipAddress)){
            heartbeatIpList.put(ipAddress,new Date());
        }
        return ResultUtil.success();
    }
    @RequestMapping("/activityStatistical")
    public Result activityStatistical(){
        Map<String,Object> result=loginService.activityStatistical();
        return ResultUtil.success(result);
    }
    @GetMapping("/getConfig")
    public Result getConfig(String key) {
        return ResultUtil.success(loginService.getConfig(key));
    }
    @PostMapping("/setConfig")
    public Result setConfig(@RequestBody Map<String,String> map) {
        String key=map.get("key");
        String value=map.get("value");
        if(StringUtils.isEmpty(key)||StringUtils.isEmpty(value)){
            return ResultUtil.error(-1,"参数错误");
        }
        loginService.setConfig(key,value);
        return ResultUtil.success();
    }

}
